package com.jingli.pile.controller;

import com.alibaba.fastjson2.JSONObject;
import com.jingli.pile.configuration.PushProperties;
import com.jingli.pile.domain.CommonDataResult;
import com.jingli.pile.domain.NotifyEquipChargeStatusResult;
import com.jingli.pile.domain.StationStatusInfoData;
import com.jingli.pile.entity.*;
import com.jingli.pile.request.StationQueryRequest;
import com.jingli.pile.request.StationStatusQueryRequest;
import com.jingli.pile.request.TokenQueryRequest;
import com.jingli.pile.service.PlatformInfoService;
import com.jingli.pile.service.SupStationStatusInfoService;
import com.jingli.pile.util.HmacMd5Signature;
import com.jingli.pile.util.MessageEncryption;
import com.jingli.pile.util.PileSignUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping
public class StationStatusApiController {
    private Logger logger = LoggerFactory.getLogger(StationStatusApiController.class);
    @Autowired
    private SupStationStatusInfoService stationStatusInfoService;
    @Autowired
    private PushProperties pushProperties;

    @RequestMapping("/evcs/v1/supervise_query_station_status")
    @ResponseBody
    public CommonDataResult queryStationStatus(@RequestBody TokenQueryRequest queryRequest) throws Exception {
        logger.info("查询充换电站状态信息参数:{}",JSONObject.toJSONString(queryRequest));
        String decrypted = MessageEncryption.decrypt(queryRequest.getData(), pushProperties.getDataSecret(), pushProperties.getDataSecretIV());
        logger.info("查询充换电站状态信息解密后:{}",decrypted);
        StationStatusQueryRequest statusQueryRequest = JSONObject.parseObject(decrypted, StationStatusQueryRequest.class);
        logger.info("转换后的换电站状态信息查询结果:{}",JSONObject.toJSONString(statusQueryRequest));
        List<SupStationStatusInfo> supStationStatusInfos = stationStatusInfoService.queryStationStatus(statusQueryRequest);
        StationStatusInfoData infoData = new StationStatusInfoData();
        infoData.setStationStatusInfos(supStationStatusInfos);
        String signParams = PileSignUtils.signParams(JSONObject.toJSONString(infoData), pushProperties.getDataSecret(), pushProperties.getDataSecretIV());
        String sign = HmacMd5Signature.hmacMd5(signParams,pushProperties.getSignSecret());
        CommonDataResult result = new CommonDataResult();
        result.setRet(0);
        result.setMsg("");
        result.setData(signParams);
        result.setSig(sign);
        return result;
    }

    @RequestMapping("/evcs/v1/supervise_notification_station_status")
    @ResponseBody
    public Integer notificationStationStatus(@RequestBody SupConnectorStatusInfo supConnectorStatusInfo){
        logger.info("收到电设备接口状态信息：{}", JSONObject.toJSONString(supConnectorStatusInfo));
        return 0;
    }
    //推送充电状态信息
    @RequestMapping("/evcs/v1/supervise_notification_equip_charge_status")
    @ResponseBody
    public NotifyEquipChargeStatusResult notificationEquipChargeStatus(@RequestBody SupEquipChargeStatus supEquipChargeStatus){
        logger.info("收到推送充电状态信息：{}", JSONObject.toJSONString(supEquipChargeStatus));
        return NotifyEquipChargeStatusResult.sucess(supEquipChargeStatus.getOrderNo());
    }

    /**
     * 充换电站用能统计信息
     * @param supStationStatsInfo
     * @return
     */
    @RequestMapping("/evcs/v1/supervise_notification_operation_stats_info")
    @ResponseBody
    public Integer notificationOperatorStats(@RequestBody SupStationStatsInfo supStationStatsInfo){
        logger.info("收到充换电站用能统计信息：{}", JSONObject.toJSONString(supStationStatsInfo));
        return 0;
    }
    //电站充电订单信息
    @RequestMapping("/evcs/v1/supervise_notification_charge_order_info")
    @ResponseBody
    public Integer notificationChargeOrderInfo(@RequestBody SupChargeOrderInfo subChargeOrderInfo){
        logger.info("收到充换电站用能统计信息：{}", JSONObject.toJSONString(subChargeOrderInfo));
        return 0;
    }
}
